var viewer = {
		id: null,
		displayName: null,
		thumbnailUrl: null
	};

var div = $("#content_div");

function startWorkflow() {

	var xml_string = '<html><head>Head</head><body>Body</body></html>';
	var json_obj = xml2json.parser(xml_string);
	
					$.jsonp({
					  url: "http://gdata.youtube.com/feeds/api/users/108247582772798918203?callback=?",
					  data: {
						  alt: "json-in-script"
					  },
					  success: function(userProfile) {
						  // handle user profile here 
						  alert("success");
					  },
					  error: function(d,msg) {
						  alert("Could not find user ");
					  }
					});
					
	fetchViewerData();
}

function fetchViewerData() {
		osapi.people.getViewer().execute(function (result) {
		if (result.error) {
			div.innnerHTML = result.error.message;
		} else {
			viewer.id = result.id;
			viewer.displayName = result.displayName;
			viewer.thumbnailUrl = result.thumbnailUrl;
			showParticipants();
		}
	});
}

function showParticipants() {
	osapi.people.getViewerFriends().execute(function (result) {
		if (result.error) {
			div.innnerHTML = result.error.message;
		} else {
			// clear div
			div.empty();
			
			// append current viewer (not included in viewerFriends)
			addOrUpdateParticipantInformation(viewer);

			for (var n in result) {
			  var list = result[n];
			  for (var p in list) {
				addOrUpdateParticipantInformation(list[p]);
				gadgets.window.adjustHeight();
			  }
			}

			updateViewersTimestamp();
			fetchParticipantInformation();
			gadgets.window.adjustHeight();
		  }
		});
}
	
function addOrUpdateParticipantInformation(participant) {
		var img = $("#img" + participant.id);

		if (!img.length) {
		  img = $("<img src=" + participant.thumbnailUrl + " id=" + "img"+participant.id + " />");
		  div.append(img);
		  div.append(document.createTextNode(" "));
		}

		if (participant.id == viewer.id) {
		  lastSeen = "now";
		} else {
		  if (typeof participant.ts === 'undefined') {
			lastSeen = "never";
		  } else {
			lastSeen = new Date(participant.ts).toLocaleString();
		  }
		}

		img.attr("title", participant.displayName + " (last seen: " + lastSeen +")");
}
  
function updateViewersTimestamp() {
		osapi.appdata.update({
		  userId: '@viewer',
		  data: {
			id: viewer.id,
			ts: (new Date()).toGMTString(),
			displayName: viewer.displayName,
			thumbnailUrl: viewer.thumbnailUrl
		  }
		}).execute(function (userData) {
		  if (userData.error){
			div.innnerHTML = userData.error.message;
		  } else {
			streamwork.pbe.setAppdataUpdateHandler(updateHandler);
		  }
		});
}
  
function updateHandler(userData) {
		for (var n in userData) {
		  addOrUpdateParticipantInformation(userData[n]);
		}
}
  
function fetchParticipantInformation() {
		osapi.appdata.get({
		  groupId: '@friends',
		}).execute(function(userData) {
		  if (userData.error) {
			div.innnerHTML = userData.error;
		  } else {
			for (var n in userData) {
			  addOrUpdateParticipantInformation(userData[n]);
			}
		  }
		});
}
  
gadgets.util.registerOnLoadHandler(startWorkflow);